package interactive.focusPhoto 
{
	import interactive.focusPhoto.item.PhotoItem;
	import caurina.transitions.Tweener;
	import interactive.focusPhoto.item.SwfItem;
	/**
	 * 整体缓动的焦点图
	 * @author Ning
	 */
	public class TotalTweenFocusPhoto extends TotalFocusPhoto 
	{
		//缓动时间
		protected var tweenTime:Number;
		//当前元素
		protected var curItem:PhotoItem;
		//上次元素
		protected var lastItem:PhotoItem;
		public function TotalTweenFocusPhoto(itemClass:Class=null,itemWidth:Number=100,itemHeight:Number=100,tweenTime:Number=1,delay:Number=5000) 
		{
			this.tweenTime = tweenTime;
			super(itemClass, itemWidth, itemHeight, delay);
		}
		override public function create():void 
		{
			super.create();
			//把所有元素添加到显示列表上
			for (var i:int = 0; i < items.length; i++ )
			{
				var item:PhotoItem = getItemAt(i);
				item.x = i * item.itemWidth;
				holder.addChild(item);
			}
		}
		override public function showAt(index:int):void 
		{
			super.showAt(index);
			var item:PhotoItem = getItemAt(index);
			if (!curItem) {
				curItem = item;
				holder.x = 0;
				curItem.autoPlay = true;
			}
			else {
				Tweener.removeTweens(holder);
				stopLastItem();
				lastItem = curItem;
				curItem = item;
				curItem.stop();
				var x:Number = -1 * itemWidth * index;
				Tweener.addTween(holder, { time:tweenTime, x:x,onComplete:onComplte } );
			}
			if (!curItem.isLoad) {
			    curItem.load();
				curItem.autoPlay = true;
			}
			
			function onComplte():void
			{
				stopLastItem();
				lastItem = null;
				curItem.play();
			}
			function stopLastItem():void
			{
				if (lastItem) {
					lastItem.stop();
					lastItem.autoPlay = false;
				}
			}
		}
	}

}